package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/07/14 10:15
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
public class CustomControllerAdvice {

    // 该注解对应的方法可以ModelAndView相关的，也可以是Json → 写法和Handler方法一样
    // ModelAndView
    //@ExceptionHandler(ArithmeticException.class)
    /*public ModelAndView arithmeticExceptionResolve() {
        return new ModelAndView();
    }*/
    /*public String arithmeticExceptionResolve() {
        return "/exception.jsp";
    }*/

    // 通过形参可以直接获得异常对象
    @ExceptionHandler(ArithmeticException.class)
    //@ResponseBody
    public BaseRespVo arithmeticExceptionResolve(ArithmeticException exception) {
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setMsg(exception.getMessage());//异常的msg
        baseRespVo.setStatus(500);
        return baseRespVo;
    }
}
